package com.dcbeida.activity.source_unit;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.SourceListAdapter;
import com.dcbeida.bean.SourceBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 来货单位选择
 * Sean
 */

public class SourceActivity extends BaseActivity implements SourceContract.View {
    @Bind(R.id.img_back)
    ImageView img_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.et_query)
    EditText etQuery;
    @Bind(R.id.myList)
    ListView myList;
    private SourcePresenter mPresent;
    private SourceListAdapter adapter;
    @Bind(R.id.lin_empty)
    LinearLayout lin_empty;

    @Override
    protected int getLayoutResId() {
        return R.layout.hospital_listview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParams();
        initData();
    }

    @OnClick(R.id.img_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }

    private void initParams() {
        tvTitle.setText("来货单位选择");
        etQuery.setHint("输入来货单位名称查询");
        mPresent = new SourcePresenter(this, this);
        mPresent.getSourceList();
    }

    private void initData() {
        etQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(etQuery.getText().toString().trim())) {
                    mPresent.getSourcelInfo(etQuery.getText().toString().trim());
                } else {
                    mPresent.getSourceList();
                }
            }
        });
        adapter = new SourceListAdapter(SourceActivity.this);
        myList.setEmptyView(lin_empty);
        myList.setAdapter(adapter);
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                SourceBean SourceBean = (SourceBean) adapter.getItem(position);
                bundle.putSerializable("sourceBean", SourceBean);
                intent.putExtras(bundle);
                SourceActivity.this.setResult(RESULT_OK, intent);
                SourceActivity.this.finish();
            }
        });
    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showList(ArrayList<SourceBean> list) {
        adapter.setmArrayList(list);
    }

    @Override
    public void setPresenter(SourceContract.Present presenter) {

    }
}
